package com.teamwhole.modules.oa.service;

import java.util.List;
import java.util.Map;

import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Maps;
import com.teamwhole.common.config.Global;
import com.teamwhole.common.utils.StringUtils;
import com.teamwhole.core.persistence.service.CrudService;
import com.teamwhole.modules.act.service.ActTaskService;
import com.teamwhole.modules.oa.dao.WorkOrderDao;
import com.teamwhole.modules.oa.entity.WorkOrder;
import com.teamwhole.modules.oa.model.WorkOrderEnum;
import com.teamwhole.modules.sys.entity.Department;
import com.teamwhole.modules.sys.entity.User;
import com.teamwhole.modules.sys.service.DepartmentService;

@Service
public class WorkOrderService extends CrudService<WorkOrderDao, WorkOrder> {
	
	@Autowired
	private ActTaskService actTaskService;
	
	@Autowired
	private DepartmentService deptService;
	
	/**
	 * 工单创建
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public void create(WorkOrder entity) {
		entity.preInsert();
		dao.insert(entity);
		
		// 启动流程(流程定义Key/业务表/业务表ID/标题/变量)
		Map<String, Object> vars = Maps.newHashMap();
		vars.put("dept", getDistributes());
		actTaskService.startProcess("WorkOrder", "oa_work_order", entity.getId(), entity.getContent(), vars, entity.getCreator().getLoginName());
	}
	
	/**
	 * 工单派发
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public void distribute(WorkOrder entity) {
		entity.setStatus(WorkOrderEnum.DISTRIBUTE.getCode());
		dao.updateStatus(entity);
		String deptId = entity.getParamMap().get("dept").toString();
		Department dept = deptService.get(deptId);
		Map<String, Object> vars = Maps.newHashMap();
		// 设置意见
		entity.getAct().setComment("[派发]" + dept.getName());
		vars.put("dept", deptId);
		vars.put("user", null);
		actTaskService.complete(entity.getAct().getTaskId(), entity.getAct().getProcInsId(), entity.getAct().getComment(), vars);
	}
	
	/**
	 * 工单处理
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public void process(WorkOrder entity, User user) {
		entity.setStatus(WorkOrderEnum.PROCESSING.getCode());
		dao.updateStatus(entity);
		
		entity.getAct().setComment("[处理]" + entity.getParamMap().get("commit"));
		Map<String, Object> vars = Maps.newHashMap();
		// 图片
		vars.put("attachments", entity.getParamMap().get("attachments"));
		vars.put("user", user.getLoginName());
		vars.put("dept",  user.getDept().getId());
		vars.put("pass", entity.getParamMap().get("pass"));
		actTaskService.complete(entity.getAct().getTaskId(), entity.getAct().getProcInsId(), entity.getAct().getComment(), vars);
	}
	
	/**
	 * 提交审核
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public void toAudit(WorkOrder entity, User user) {
		entity.setStatus(WorkOrderEnum.AUDIT.getCode());
		dao.updateStatus(entity);
		
		entity.getAct().setComment("[提交审核]" + entity.getParamMap().get("commit"));
		// 当前处理用户
		Map<String, Object> vars = Maps.newHashMap();
		// 图片
		vars.put("attachments", entity.getParamMap().get("attachments"));
		vars.put("dept", getAudits());
		vars.put("processUser", user.getLoginName());
		vars.put("processDept", user.getDept().getId());
		vars.put("pass", entity.getParamMap().get("pass"));
		actTaskService.complete(entity.getAct().getTaskId(), entity.getAct().getProcInsId(), entity.getAct().getComment(), vars);
	}
	
	/**
	 * 工单回退
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public void back(WorkOrder entity) {
		entity.setStatus(WorkOrderEnum.DISTRIBUTE.getCode());
		dao.updateStatus(entity);
		
		entity.getAct().setComment("[回退]" + entity.getParamMap().get("commit"));
		Map<String, Object> vars = Maps.newHashMap();
		// 获取工单派发部门
		Department dept = new Department();
		dept.getParamMap().put("isDistribute", "1");
		List<Department> list = deptService.findList(dept);
		if (list.size() > 0) {
			vars.put("dept", list.get(0).getId());
		}
		vars.put("pass", entity.getParamMap().get("pass"));
		actTaskService.complete(entity.getAct().getTaskId(), entity.getAct().getProcInsId(), entity.getAct().getComment(), vars);
	}
	
	/**
	 * 工单审核
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public void audit(WorkOrder entity) {
		// 通过
		if (Global.YES.equals(entity.getParamMap().get("pass").toString())) {
			entity.setStatus(WorkOrderEnum.FINISH.getCode());
		} else { // 驳回
			entity.setStatus(WorkOrderEnum.PROCESSING.getCode());
		}
		dao.updateStatus(entity);
		
		entity.getAct().setComment("[审核]" + entity.getParamMap().get("commit"));
		Task task = actTaskService.getTaskIncludeVars(entity.getAct().getTaskId());
		Map<String, Object> vars = Maps.newHashMap();
		vars.put("user", task.getProcessVariables().get("processUser"));
		vars.put("dept",  task.getProcessVariables().get("processDept"));
		vars.put("pass", entity.getParamMap().get("pass"));
		actTaskService.complete(entity.getAct().getTaskId(), entity.getAct().getProcInsId(), entity.getAct().getComment(), vars);
	}
	
	/**
	 * 获取派发部门
	 * @return
	 */
	private String getDistributes() {
		Department dept = new Department();
		dept.getParamMap().put("isDistribute", Global.YES);
		dept.getParamMap().put("isFacility", Global.NO);
		List<Department> list = deptService.findList(dept);
		String deptIds = "";
		for (Department item : list) {
			if (StringUtils.isBlank(deptIds)) {
				deptIds = item.getId();
			} else {
				deptIds += ("," + item.getId());
			}
		}
		return deptIds;
	}
	
	/**
	 * 获取工单审核 部门
	 * @return
	 */
	private String getAudits() {
		Department dept = new Department();
		dept.getParamMap().put("isAudit", Global.YES);
		List<Department> list = deptService.findList(dept);
		String deptIds = "";
		for (Department item : list) {
			if (StringUtils.isBlank(deptIds)) {
				deptIds = item.getId();
			} else {
				deptIds += ("," + item.getId());
			}
		}
		return deptIds;
	}
	
}
